package 标准接口;



import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


 
public class UpdateRequestinfo {
 
//第一步做一次就行了  记录下来
		 /**
		 * 模拟缓存服务
		 */
		 private static final Map<String,String> SYSTEM_CACHE = new HashMap <>(); 
		 
		 /**
		 * ecology 系统发放的授权许可证(appid)
		 */
		 private static final String APPID = "EEAA5436-7577-4BE0-8C6C-89E9D44801EA";
		 
		 
		 public static void main(String[] args) {
 
		// System.out.println(jsonsss.toString());

		 //testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/getRequestResources","requestid=686001");
			 HttpRequest.closeCookie();
		 testRestful("http://oa.maxwell-gp.com.cn:8090","/api/workflow/paService/submitRequest","");
		  

		 
		 }
		 /**
		 * 第一步：
		 *
		 * 调用 ecology 注册接口,根据 appid 进行注册,将返回服务端公钥和 Secret 信息
		 */
 
		 public static Map<String,Object> testGetoken(String address){
			 // 从系统缓存或者数据库中获取 ECOLOGY 系统公钥和 Secret 信息




			 String secret ="6a393956-42c1-4b75-8b77-91c342ed90f1";
			 String spk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCmIqeh3HlAxMNSjepuY1YsRj6pqfX/r88v1xOKI7+vIXDpMLgmaA8ytRGTyHaxscXiXxcCKE6T+z54qrxaxw6b4/WSfcH8QaghHhQRAtvMWxP/SC1Vg5dniQMUVvrsB2wbf0DfyiftGeBf8gJbe1uWsXrMZLZE3olrDMCjylo8SwIDAQAB";

			 SYSTEM_CACHE.put("SERVER_PUBLIC_KEY",spk);
			 //ECOLOGY 返回的系统密钥
			 SYSTEM_CACHE.put("SERVER_SECRET",secret);

			 
			 RSA rsa = new RSA(null,spk);
			 //对秘钥进行加密传输，防止篡改数据
			 String encryptSecret = rsa.encryptBase64(secret,CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);
			 //调用 ECOLOGY 系统接口进行注册
			 String data = HttpRequest.post(address+ "/api/ec/dev/auth/applytoken")
			 .header("appid",APPID)
			 .header("secret",encryptSecret)
			 .header("time","3600")
			 .execute().body();
			 System.out.println("testGetoken()："+data);
			 Map<String,Object> datas = JSONUtil.parseObj(data);
			 //ECOLOGY 返回的 token
			 // TODO 为 Token 缓存设置过期时间
			 SYSTEM_CACHE.put("SERVER_TOKEN",StrUtil.nullToEmpty((String)datas.get("token")));
			 return datas;
			 }
		 
		 public static String testRestful(String address,String api,String jsonParams){
			 //ECOLOGY 返回的 token
			 String token= SYSTEM_CACHE.get("SERVER_TOKEN");
			 if (StrUtil.isEmpty(token)){
			 token = (String) testGetoken(address).get("token");
			 }
		 
			 
			 
			 
			 
			 String spk = SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
			 
			 //
			 //System.out.println(spk);
			 RSA rsa = new RSA(null,spk);
			 String encryptUserid = rsa.encryptBase64("243",CharsetUtil.CHARSET_UTF_8,KeyType.PublicKey);

			 Map<String, Object> params = new HashMap<>();
			 params.put("requestId", "1038373");
			 params.put("mainData", "[{'fieldValue':'eeee','fieldName':'prdno'}]");

			 Map<String, String> otherParams = new HashMap<>();
			 otherParams.put("src","save");
			 params.put("otherParams", JSONObject.toJSONString(otherParams));

			 String data = HttpRequest.post(address + api)
					 .header("appid",APPID)
					 .header("token",token)
					 .header("userid",encryptUserid)
					 .contentType("application/x-www-form-urlencoded")
					 .form(params)
					 .execute().body();
					 //.closeCookie();

			 System.out.println(data);
			// String data=null;
			 return data;
			 }

		 

}
